package main

import (
	"fmt"
	"time"
)

func main(){
	// basic usage
	ch:=make(chan int,1)
	for i:=0;i<4;i++{
		select{
		case e,ok:=<-ch:
			if !ok{
				fmt.Println("End.")
				goto NEXT
			}
			fmt.Println(e)
			close(ch)
		default:
			fmt.Println("No Data!")
			ch<-1
		}
	}
	NEXT:
	// timeout handle
	ch1:=make(chan string)
	ch2:=make(chan string)
	go func(){
		time.Sleep(time.Second*1)
		ch1<-"left"
	}()
	go func(){
		ch2<-"right"
	}()
	select{
		case msg1 := <-ch1:
			fmt.Println("received",msg1)
		case msg2 := <-ch2:
			fmt.Println("received",msg2)
	}
}